package com.softgroup.breath.easyecgproject.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2017/12/21.
 */

public class NumberRulerView extends View {
    private int mWidth;
    private int mHeight;
    private Paint textPaint;
    private Paint numberPaint;
    private Paint rulerNumberPaint;
    private Paint rulerPaint;
    private Paint cursorPaint;
    private float progress;
    private int min;
    private int max;
    private String unit;
    private String text;
    private int textSize;
    private int value;
    private float cursor;
    private RectF rect;
    private Path cursorPath;

    public NumberRulerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setColor(0xff00afec);
        numberPaint = new Paint();
        numberPaint.setAntiAlias(true);
        numberPaint.setTextAlign(Paint.Align.CENTER);
        numberPaint.setColor(0xff00afec);
        rulerNumberPaint = new Paint();
        rulerNumberPaint.setAntiAlias(true);
        rulerNumberPaint.setTextAlign(Paint.Align.CENTER);
        rulerNumberPaint.setColor(0xff00afec);
        rulerPaint = new Paint();
        rulerPaint.setAntiAlias(true);
        rulerPaint.setTextAlign(Paint.Align.CENTER);
        rulerPaint.setColor(0xffb6cdd9);
        rulerPaint.setStyle(Paint.Style.STROKE);
        cursorPaint = new Paint();
        cursorPaint.setAntiAlias(true);
        cursorPaint.setColor(0xfff39700);
        cursorPaint.setTextAlign(Paint.Align.CENTER);
        cursorPath = new Path();
        min = 0;
        max = 100;
        value = 50;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        textPaint.setTextSize(mHeight / 10);
        numberPaint.setTextSize(mHeight / 6);
        rulerNumberPaint.setTextSize(mHeight / 30);
        rulerPaint.setTextSize(mHeight / 15);
        rect = new RectF(mWidth / 24, mHeight / 2, mWidth * 23 / 24, mHeight * 3 / 4);
        cursorPaint.setTextSize(mHeight / 16);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRoundRect(rect, mWidth / 60, mWidth / 60, rulerPaint);
        for (int i = 0; i < 11; i++) {
            if (i < 10) {
                canvas.drawLine(mWidth / 12 + i * mWidth / 12 + mWidth / 24, mHeight / 2, mWidth / 12 + i * mWidth / 12 + mWidth / 24, mHeight * 13 / 24, rulerPaint);
            }
            canvas.drawLine(mWidth / 12 + i * mWidth / 12, mHeight / 2, mWidth / 12 + i * mWidth / 12, mHeight * 7 / 12, rulerPaint);
            canvas.drawText(String.format("%.1f", min + (max - min) * i / 10f), mWidth / 12 + i * mWidth / 12, mHeight * 15 / 24, rulerNumberPaint);

        }
        canvas.drawText("▼", (value - min) * mWidth * 10 / 12 / (max - min) + mWidth / 12, mHeight / 2, cursorPaint);
        canvas.drawCircle(mWidth / 12, mHeight * 7 / 8, mHeight / 16, rulerPaint);
        canvas.drawCircle(mWidth * 11 / 12, mHeight * 7 / 8, mHeight / 16, rulerPaint);
        canvas.drawText("-", mWidth / 12, mHeight * 7 / 8 + mHeight / 32, textPaint);
        canvas.drawText("+", mWidth * 11 / 12, mHeight * 7 / 8 + mHeight / 32, textPaint);

//        cursorPath.reset();
//        cursorPath.moveTo(cursor,mHeight/2);
//        cursorPath.lineTo();

        canvas.drawText(text, mWidth / 2, mHeight / 6, textPaint);
        canvas.drawText(value + unit, mWidth / 2, mHeight / 3, numberPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        if (event.getAction() == MotionEvent.ACTION_DOWN | event.getAction() == MotionEvent.ACTION_MOVE) {
            if (y > mHeight / 3 & y < mHeight * 3 / 4) {
                if (x > mWidth / 12 & x < mWidth * 11 / 12) {
                    setProgress(x);
                    setValue();
                }
            }
            if (y > mHeight * 3 / 4) {
                if (x < mWidth * 2 / 12) {
                    sub();
                } else if (x > mWidth * 10 / 12) {
                    add();
                }
            }
            if (onNumberChangeListener != null) {
                onNumberChangeListener.onNumberChange(value);
            }
            postInvalidate();
        }
        if (event.getAction() == MotionEvent.ACTION_OUTSIDE | event.getAction() == MotionEvent.ACTION_UP) {
            postInvalidate();
            return false;
        }
        return true;
    }

    public int getValue() {
        return value;
    }

    public void setValue() {
        value = Math.round(progress * (max - min) + min);
    }

    public void setValue(int value) {
        this.value = value;
        postInvalidate();
    }

    private void add() {
        if (value < max) {
            value++;
        }
    }

    private void sub() {
        if (value > min) {
            value--;
        }
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public float getProgress() {
        return progress;
    }

    private void setProgress(float x) {
        progress = (x - mWidth / 12) / (mWidth * 10 / 12);
    }

    public void setText(String text) {
        this.text = text;
    }

    private OnNumberChangeListener onNumberChangeListener;

    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    public interface OnNumberChangeListener {
        void onNumberChange(int number);
    }
}
